package activities;

import helper.ExercisesEnum;
import helper.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.intellifit.R;

public class ExerciseInformationActivity extends Activity {

	public static final String HTTPS = "https://";
	public static final String HTTP = "http://";

	public static final String BICEP_CURLS_URL = "http://www.youtube.com/watch?v=uO_CNYidOw0";
	public static final String SQUATS_URL = "http://www.youtube.com/watch?v=UXJrBgI2RxA";
	public static final String SIT_UPS_URL = "http://www.youtube.com/watch?v=1fbU_MkV7NE";

	private ExercisesEnum exType = null;
	private boolean showAgain = false;

	// view components
	private TextView headline;
	private ImageView exImg, exHoldingMobileImg;
	private TextView exDescr;
	private TextView exExec, howToDesc;
	private Button webLinkBtn, startBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exercise_information);

		this.exType = (ExercisesEnum) getIntent().getSerializableExtra(
				"ExerciseType");

		createViewComponents();
		final CheckBox chk = (CheckBox) findViewById(R.id.checkAgainChk);
		
		chk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (chk.isChecked()) {
					showAgain = false;
				} else {
					showAgain = true;
				}
			}
		});

		findViewById(R.id.startExercise).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						submitMessage();
						finish();
					}
				});
	}

	private void createViewComponents() {
		this.headline = (TextView) (findViewById(R.id.heading));
		this.exImg = (ImageView) findViewById(R.id.exerciseImg);
		this.exHoldingMobileImg = (ImageView) findViewById(R.id.exerciseHoldMobileImg);
		this.webLinkBtn = (Button) findViewById(R.id.openWebLinkBtn);
		this.exDescr = (TextView) findViewById(R.id.exerciseDescr);
		this.howToDesc = (TextView) findViewById(R.id.howToDesc);
		
		this.startBtn = (Button) findViewById(R.id.startExercise);
		//this.exExec = (TextView) findViewById(R.id.exerciseExec);
		if (this.exType.equals(ExercisesEnum.BICEP_CURLS)) {
			this.headline.setText("EXERCISE INFO - BICEP CURLs");
			this.exImg.setImageResource(R.drawable.bicep_curls_muscles);
			this.exHoldingMobileImg.setImageResource(R.drawable.hold_desc_situps);
			this.exDescr.setText(R.string.bicep_curls_description);
			this.howToDesc.setText("Attach your phone to your forearm.");
			this.startBtn.setBackgroundResource(R.drawable.bg_curl);
			this.webLinkBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					openBrowser(ExerciseInformationActivity.this, BICEP_CURLS_URL);
				}
			});
		} else if (this.exType.equals(ExercisesEnum.SQUATS)) {
			this.headline.setText("EXERCISE INFO - SQUATS");
			this.exImg.setImageResource(R.drawable.squats_muscles);
			this.exDescr.setText(R.string.squats_description);
			//this.exExec.setText(R.string.squats_info_execution);
			this.exHoldingMobileImg.setImageResource(R.drawable.hold_desc_squats);
			this.howToDesc.setText("Hold your phone above the chest with two hands.");
			this.startBtn.setBackgroundResource(R.drawable.bg_squat);
			
			this.webLinkBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					openBrowser(ExerciseInformationActivity.this, SQUATS_URL);
				}
			});

		}else if (this.exType.equals(ExercisesEnum.SIT_UPS)) {
			this.headline.setText("EXERCISE INFO - SIT UPs");
			this.exImg.setImageResource(R.drawable.squats_muscles);
			this.exDescr.setText(R.string.sit_ups_description);
			//this.exExec.setText(R.string.squats_info_execution);
			this.exHoldingMobileImg.setImageResource(R.drawable.hold_desc_situps);
			this.howToDesc.setText("Hold your phone above the chest with two hands.");
			this.startBtn.setBackgroundResource(R.drawable.bg_situp);
			this.webLinkBtn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					openBrowser(ExerciseInformationActivity.this, SIT_UPS_URL);
				}
			});

		}
		//fix layout for smaller screen resolutions
		if(Settings.DISPLAY_HEIGHT <=800){
		webLinkBtn.setTextSize(13);
		startBtn.setTextSize(13);		
		((CheckBox) findViewById(R.id.checkAgainChk)).setTextSize(13);
		
		}
		
		
	}

	@Override
	public void onBackPressed() {
		submitMessage();
		super.onBackPressed();

	}

	public void submitMessage() {
		// get the Entered message
		Intent intentMessage = new Intent();

		// put the message in Intent
		intentMessage.putExtra("ExerciseType", exType);
		intentMessage.putExtra("showAgain", showAgain);

		// Set The Result in Intent
		setResult(2, intentMessage);
	}

	public static void openBrowser(final Context context, String url) {

		if (!url.startsWith(HTTP) && !url.startsWith(HTTPS)) {
			url = HTTP + url;
		}

		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
		context.startActivity(Intent.createChooser(intent, "Chose browser"));

	}

}
